﻿using System;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Text;

namespace JESAI.Core.Websocket.Client.Models
{
    /// <summary>
    /// 有关发生断开连接的信息
    /// </summary>
    public class DisconnectionInfo
    {
        /// <summary>
        /// 有关发生断开连接的信息
        /// </summary>
        /// <param name="type"></param>
        /// <param name="closeStatus"></param>
        /// <param name="closeStatusDescription"></param>
        /// <param name="subProtocol"></param>
        /// <param name="exception"></param>
        public DisconnectionInfo(DisconnectionType type, WebSocketCloseStatus? closeStatus,
          string? closeStatusDescription, string? subProtocol, Exception? exception)
        {
            Type = type;
            CloseStatus = closeStatus;
            CloseStatusDescription = closeStatusDescription;
            SubProtocol = subProtocol;
            Exception = exception;
        }

        /// <summary>
        /// 连接断开的原因
        /// </summary>
        public DisconnectionType Type { get; }

        /// <summary>
        /// 指示远程终结点启动关闭握手的原因
        /// </summary>
        public WebSocketCloseStatus? CloseStatus { get; }

        /// <summary>
        /// 允许远程端点描述连接关闭的原因
        /// </summary>
        public string? CloseStatusDescription { get; }

        /// <summary>
        /// 在握手开始时协商的子协议
        /// </summary>
        public string? SubProtocol { get; }

        /// <summary>
        /// 导致断开连接的异常，可以为空
        /// </summary>
        public Exception? Exception { get; }

        /// <summary>
        /// 如果要取消正在进行的重新连接，请设置为true
        /// </summary>
        public bool CancelReconnection { get; set; }

        /// <summary>
        /// 如果要取消正在进行的连接关闭，请设置为true（仅当Type=ByServer时）
        /// </summary>
        public bool CancelClosing { get; set; }

        /// <summary>
        /// 简单工厂方法，用于创建DisconnectionInfo对象
        /// </summary>
        public static DisconnectionInfo Create(DisconnectionType type, WebSocket? client, Exception? exception)
        {
            return new DisconnectionInfo(type, client?.CloseStatus, client?.CloseStatusDescription,
                client?.SubProtocol, exception);
        }
    }
}
